<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/25
 * Time: 20:03
 */
namespace app\admin\controller;
use think\Db;
class FeedbackController extends CommonController
{
    /**
     * 获得数据
     */
    public function getFeedBackData()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = array();
        if(isset($_POST['keyword']) && $_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['ef.feedback_weixin|eu.user_username'] = array('like',"%$keyword%");
        }
        if(isset($_POST['feedback_type']) && $_POST['feedback_type']!=""){
            $types = $_POST['feedback_type'];
            $where['ef.feedback_type'] = array('like',"%$types%");
        }
        $arr = Db::table('ew_feedback')
            ->alias ('ef')
            ->join('ew_u_user eu','ef.feedback_user_id=eu.user_id','left')
            ->where($where)
            ->field('eu.user_id,eu.user_username,ef.*')
            ->limit($rows * ($page - 1), $rows)
            ->order('feedback_time DESC')
            ->select();
        for($i=0;$i<count($arr);$i++){
            $num = explode(',',$arr[$i]['feedback_type']);
            $arr[$i]['feedback_type'] = $num;
        }
        for($a=0;$a<count($arr);$a++){
            for($b=0;$b<count($arr[$a]['feedback_type']);$b++){
                $k = Db::table('ew_feedback_type')->where('fb_id',$arr[$a]['feedback_type'][$b])->value('fb_name');
                $arr[$a]['fb_name'][] = $k;
            }
           $arr[$a]['fb_name'] = implode(',',$arr[$a]['fb_name']);
        }
        $total = Db::table('ew_feedback')
            ->alias ('ef')
            ->join('ew_u_user eu','ef.feedback_user_id=eu.user_id','left')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $arr;
        return json_encode($data);
    }

    /**
     * deleteFeedback 删除反馈
     * @return 状态
     */
    public function deleteFeedback(){
        $id = $_POST['id'];
        $res = Db::table('ew_feedback')->delete($id);
        if($res){
            return json_encode(['status'=>1,'msg'=>"删除成功！"]);
        }else{
            return json_encode(['status'=>0,'msg'=>"删除失败！"]);
        }
    }

    public function lookFeedback(){
        $arr = Db::table('ew_feedback')
            ->alias ('ef')
            ->join('ew_u_user eu','ef.feedback_user_id=eu.user_id','left')
            ->where('feedback_id',$_POST['id'])
            ->field('eu.user_id,eu.user_username,ef.*')
            ->find();
        $num = explode(',',$arr['feedback_type']);
        $arr['feedback_type'] = $num;
        for($b=0;$b<count($arr['feedback_type']);$b++){
            $k = Db::table('ew_feedback_type')->where('fb_id',$arr['feedback_type'][$b])->value('fb_name');
            $arr['fb_name'][] = $k;
        }
        $arr['fb_name'] = implode(',',$arr['fb_name']);
        $this->assign ('arr',$arr);
        return $this->fetch("feedback/look");
    }

    /**
     * addResult 展示页面
     * @return HTML
     */
    public function addResult(){
        $this->assign ('id',$_POST['id']);
        return $this->fetch('feedback/addresult');
    }

    /**
     * add 添加处理结果
     * @return 状态
     */
    public function add(){
        extract($_POST);
        if($feedback_result==""){
            return json_encode(['status'=>0,'msg'=>"处理结果不能为空！"]);
        }else{
            $data = [
                'feedback_result'=>$feedback_result,
                'feedback_status'=>2,
                'feedback_result_time'=>date("Y-m-d H:i:s")
            ];
            $res = Db::table('ew_feedback')->where('feedback_id',$feedback_id)->update($data);
            if($res){
                return json_encode(['status'=>1,'msg'=>"处理完成！"]);
            }else{
                return json_encode(['status'=>0,'msg'=>"处理失败！"]);

            }
        }
    }


    /**
     * addResult 展示页面
     * @return HTML
     */
    public function editResult(){
        $data = Db::table('ew_feedback')->where('feedback_id',$_POST['id'])->value('feedback_result');
        $this->assign ('id',$_POST['id']);
        $this->assign ('feedback_result',$data);
        return $this->fetch('feedback/edit');
    }


    /**
     * edit 修改处理结果
     * @return 状态
     */
    public function edit(){
        extract($_POST);
        if($feedback_result==""){
            return json_encode(['status'=>0,'msg'=>"处理结果不能为空！"]);
        }else{
            $data = [
                'feedback_result'=>$feedback_result,
                'feedback_result_time'=>date("Y-m-d H:i:s")
            ];
            $res = Db::table('ew_feedback')->where('feedback_id',$feedback_id)->update($data);
            if($res){
                return json_encode(['status'=>1,'msg'=>"修改完成！"]);
            }else{
                return json_encode(['status'=>0,'msg'=>"修改失败！"]);

            }
        }
    }

}
